package com.czq.gulimail.gulimailproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czq.common.constant.product.AttrType;
import com.czq.common.utils.PageUtils;
import com.czq.common.utils.Query;
import com.czq.gulimail.gulimailproduct.dao.AttrAttrgroupRelationDao;
import com.czq.gulimail.gulimailproduct.dao.AttrDao;
import com.czq.gulimail.gulimailproduct.dao.AttrGroupDao;
import com.czq.gulimail.gulimailproduct.entity.AttrAttrgroupRelationEntity;
import com.czq.gulimail.gulimailproduct.entity.AttrEntity;
import com.czq.gulimail.gulimailproduct.entity.AttrGroupEntity;
import com.czq.gulimail.gulimailproduct.service.AttrGroupService;
import com.czq.gulimail.gulimailproduct.vo.AttrGroupRelationVO;
import com.czq.gulimail.gulimailproduct.vo.resp.AttrGroupWithAttrsResp;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {


    @Autowired
    private AttrAttrgroupRelationServiceImpl attrAttrgroupRelationService;
    @Autowired
    private AttrDao attrDao;
    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    private AttrGroupDao attrGroupDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        // 获取查询条件中的 key
        Object key = params.get("key");
        String keyStr = key != null ? key.toString().trim() : null;

        // 创建 QueryWrapper
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();

        // 如果 catelogId 不为 0，添加分类过滤条件
        if (catelogId != 0) {
            wrapper.eq("catelog_id", catelogId);
        }

        // 如果 key 不为空，添加模糊匹配条件
        if (keyStr != null && !keyStr.isEmpty()) {
            wrapper.like("attr_group_name", keyStr)
                    .or()
                    .like("descript", keyStr);
        }

        // 分页查询
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> getAttrRelation(Long attrGroupId) {
        QueryWrapper<AttrAttrgroupRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_group_id", attrGroupId);
        List<AttrAttrgroupRelationEntity> list = attrAttrgroupRelationService.list(wrapper);
        if (list != null && !list.isEmpty()) {
            List<Long> attrIds = list.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
            QueryWrapper<AttrEntity> wrapper2 = new QueryWrapper<>();
            wrapper2.in("attr_id", attrIds);
            return attrDao.selectList(wrapper2);
        }
        return new ArrayList<>();
    }

    @Override
    public void deleteRelation(List<AttrGroupRelationVO> voList) {

        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = new ArrayList<>();

        for (AttrGroupRelationVO vo : voList) {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelationEntity.setAttrGroupId(vo.getAttrGroupId());
            attrAttrgroupRelationEntity.setAttrId(vo.getAttrId());
            attrAttrgroupRelationEntities.add(attrAttrgroupRelationEntity);
        }


        attrAttrgroupRelationDao.deleteByList(attrAttrgroupRelationEntities);


    }

    @Override
    public List<AttrEntity> getAttrNotRelation(Long catlogId) {
        List<Long> attrIds = attrAttrgroupRelationDao.selectAttrIds();
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        wrapper.notIn("attr_id", attrIds).eq("attr_type", AttrType.PRODUCT_ATTR_BASE.getCode()).eq("catelog_id", catlogId);
        List<AttrEntity> list = attrDao.selectList(wrapper);
        return list == null ? new ArrayList<>() : list;
    }

    @Override
    public void saveRelation(List<AttrGroupRelationVO> vos, Long attrGroupId) {
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = new ArrayList<>();
        for (AttrGroupRelationVO vo : vos) {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelationEntity.setAttrGroupId(attrGroupId);
            attrAttrgroupRelationEntity.setAttrId(vo.getAttrId());
            attrAttrgroupRelationEntities.add(attrAttrgroupRelationEntity);
        }
        attrAttrgroupRelationDao.insert(attrAttrgroupRelationEntities);
    }

    @Override
    public List<AttrGroupWithAttrsResp> withattr(Long catelogId) {
        List<AttrGroupWithAttrsResp> attrGroupWithAttrsResps = new ArrayList<>();

        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("catelog_id", catelogId);
        List<AttrGroupEntity> attrGroupEntities = attrGroupDao.selectList(wrapper);

        attrGroupEntities.forEach(attrGroupEntity -> {
            AttrGroupWithAttrsResp attrGroupWithAttrsResp = new AttrGroupWithAttrsResp();
            BeanUtils.copyProperties(attrGroupEntity, attrGroupWithAttrsResp);

            Long attrGroupId = attrGroupEntity.getAttrGroupId();
            QueryWrapper<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntityWrapper = new QueryWrapper<>();
            attrAttrgroupRelationEntityWrapper.eq("attr_group_id", attrGroupId);
            List<Long> attrIds = new ArrayList<>();
            attrAttrgroupRelationDao.selectList(attrAttrgroupRelationEntityWrapper).forEach(attrAttrgroupRelationEntity -> {
                attrIds.add(attrAttrgroupRelationEntity.getAttrId());
            });

            QueryWrapper<AttrEntity> attrEntityQueryWrapper = new QueryWrapper<>();
            attrEntityQueryWrapper.in("attr_id", attrIds).eq("attr_type", AttrType.PRODUCT_ATTR_BASE.getCode());
            List<AttrEntity> attrEntities = attrDao.selectList(attrEntityQueryWrapper);
            attrGroupWithAttrsResp.setAttrs(attrEntities);


            attrGroupWithAttrsResps.add(attrGroupWithAttrsResp);
        });
        return attrGroupWithAttrsResps;
    }
}
